#! /usr/bin/env python3
# coding=utf-8
import xlrd
import xlwt
from xlrd import open_workbook
from xlutils.copy import copy

def excel_read(path_file='./file/alter.xls'):
    try:
        xlbook=xlrd.open_workbook(path_file)  #打开excel
    except:
        print('路径不存在该文件！')

    count=len(xlbook.sheets())            #获取excel工作簿数
    table = xlbook.sheet_by_index(0)  # 通过索引获取工作表
    nrows = table.nrows  # 获取行数
    ncols = table.ncols  # 获取列数
    print("工作簿数：%d 当前工作表行数:%d 当前工作表列数:%d" % (count, nrows, ncols))
    # 按行循环读取数据，保存到列表
    list = []
    for i in range(1, nrows):  # 从索引1开始，去除第一行
        list.append(table.row_values(i))
    return list

def excel_alter(list):
    for i in range(len(list)):
        print(list[i])
    d = list[::-1]
    return d

def excel_write(list,path_file='./file/alter.xls'): #没有默认值的参数需放前面
    #复制之前的sheel
    try:
        rb = open_workbook(path_file)  #打开excel
    except:
        print('路径不存在该文件！')

    wb = copy(rb)
    # 通过get_sheet()获取的sheet有write()方法,写入第二个excel
    # 复制之前的sheel
    try:
        ws = wb.get_sheet(1)
    except:
        print('该文件不存在第二个sheet！')
    # 写入标题栏
    title = ['大类','小类','父配置','子配置','是否推荐','配置属性','DATA_TYPE','P_CONFIG_ID','CONFIG_ID','本品','PV','需求度','成本','父装备率','子装备率','父装车率','子装车率','系统推荐','配置排序','第四步取为1的','第四步排序']
    for t in range(len(title)):
        ws.write(0, t, title[t],style())
    # 写入列表数据
    for i in range(1, len(list) + 1):
        for j in range(len(list[i - 1])):
            ws.write(i, j, list[i - 1][j])
    wb.save(path_file)

def style():
    font = xlwt.Font()  # Create the Font
    font.name = 'Times New Roman'
    font.bold = True
    font.underline = True
    font.italic = True

    borders = xlwt.Borders()  # Create Borders
    borders.left = xlwt.Borders.DASHED  # May be: NO_LINE, THIN, MEDIUM, DASHED, DOTTED, THICK, DOUBLE, HAIR, MEDIUM_DASHED, THIN_DASH_DOTTED, MEDIUM_DASH_DOTTED, THIN_DASH_DOT_DOTTED, MEDIUM_DASH_DOT_DOTTED, SLANTED_MEDIUM_DASH_DOTTED, or 0x00 through 0x0D.
    borders.right = xlwt.Borders.DASHED
    borders.top = xlwt.Borders.DASHED
    borders.bottom = xlwt.Borders.DASHED
    borders.left_colour = 0x40
    borders.right_colour = 0x40
    borders.top_colour = 0x40
    borders.bottom_colour = 0x40

    alignment = xlwt.Alignment()  # Create Alignment
    alignment.horz = xlwt.Alignment.HORZ_CENTER  # May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED
    alignment.vert = xlwt.Alignment.VERT_CENTER  # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED

    pattern = xlwt.Pattern()  # Create the Pattern
    pattern.pattern = xlwt.Pattern.SOLID_PATTERN  # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
    pattern.pattern_fore_colour = 22 # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray

    style = xlwt.XFStyle()    # Create the Style
    style.font = font         # Apply the Font to the Style
    style.borders=borders     # Add borders to Style
    style.alignment=alignment # Add alignment to Style
    style.pattern=pattern     # Add pattern to Style
    return style

a=excel_read()
b=excel_alter(a)
excel_write(b)

